package org.jeecg.modules.power.statistics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.power.device.entity.YlDevice;
import org.jeecg.modules.power.device.entity.YlDeviceRecords;
import org.jeecg.modules.power.device.mapper.YlDeviceMapper;
import org.jeecg.modules.power.device.mapper.YlDeviceRecordsMapper;
import org.jeecg.modules.power.device.service.IYlDeviceRecordsService;
import org.jeecg.modules.power.device.service.IYlDeviceService;
import org.jeecg.modules.power.order.mapper.YlPowerBankOrderMapper;
import org.jeecg.modules.power.shop.entity.YlShop;
import org.jeecg.modules.power.shop.service.IYlShopService;
import org.jeecg.modules.power.statistics.entity.YlHomeShop;
import org.jeecg.modules.power.statistics.entity.YlWeaningShop;
import org.jeecg.modules.power.statistics.entity.YlWeaningShopRecord;
import org.jeecg.modules.power.statistics.entity.YlZeroShop;
import org.jeecg.modules.power.statistics.mapper.YlHomeShopMapper;
import org.jeecg.modules.power.statistics.service.IYlHomeShopService;
import org.jeecg.modules.power.statistics.service.IYlWeaningShopRecordService;
import org.jeecg.modules.power.statistics.service.IYlWeaningShopService;
import org.jeecg.modules.power.statistics.service.IYlZeroShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: app首页门店统计表
 * @Author: jeecg-boot
 * @Date:   2024-08-01
 * @Version: V1.0
 */
@Service
public class YlHomeShopServiceImpl extends ServiceImpl<YlHomeShopMapper, YlHomeShop> implements IYlHomeShopService {



    @Autowired
    private   YlDeviceMapper ylDeviceMapper;//设备表

    @Autowired
    private YlDeviceRecordsMapper ylDeviceRecordsMapper;//设备操作记录表
    @Autowired
    private IYlShopService ylShopService;//店铺


    @Autowired
    private YlPowerBankOrderMapper ylPowerBankOrderMapper;//订单表


    @Autowired
    private  IYlZeroShopService ylZeroShopService;//0门店收益记录表


    @Autowired
    private IYlWeaningShopRecordService ylWeaningShopRecordService;//每天撤机门店记录表

    @Autowired
    private IYlWeaningShopService ylWeaningShopService;//撤机门店统计记录表


    /**
     * APP运营端-首页门店统计
     * @param bdId 业务员id
     * @param groupId 小组id
     * @param type  类型 1柜机门店数 2、0收益门店数、3 新铺门店数 4 撤机门店数 5净增门店数
     * @param mark 标记： 只有5净增门店数 的时候用得到 1 本月店铺数据  2、上个月店铺数据
     * @return
     */
    @Override
    public Integer getHomeShopQuantity(String bdId, String groupId, Integer type, Integer mark) {
        return baseMapper.getHomeShopQuantity(bdId, groupId, type, mark);
    }


//公用 新增

    /**
     *
     * @param stringShopIds 店铺id 集合
     * @param type 类型 1柜机门店数 2、0收益门店数、3 新铺门店数 4 撤机门店数 5净增门店数
     */
    public  void addHomeShopList(List<String> stringShopIds ,Integer type){
        LambdaQueryWrapper<YlShop> queryWrapper =new LambdaQueryWrapper<YlShop>();
        queryWrapper.in(YlShop::getId,stringShopIds);
        List<YlShop> ylShopList= ylShopService.list(queryWrapper);
        for (YlShop ylShop:ylShopList) {
            //生成数据  柜机门店数
            YlHomeShop ylHomeShop=new YlHomeShop();
            ylHomeShop.setBdId(ylShop.getBdId());
            ylHomeShop.setGroupId(ylShop.getOrgId());
            ylHomeShop.setType(type);
            ylHomeShop.setShopQuantity(1);
            ylHomeShop.setMark(0);
            baseMapper.insert(ylHomeShop);
        }

        if (type==2){//0门店收益  每天记录
            Date d= DateUtils.addDaysToDate(new Date(),-1);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

//            dateFormat.format(d);//转成


            for (YlShop ylShop:ylShopList) {
                //生成数据  柜机门店数
                YlZeroShop ylZeroShop =new YlZeroShop();
                ylZeroShop.setBdId(ylShop.getBdId());
                ylZeroShop.setGroupId(ylShop.getOrgId());
                ylZeroShop.setShopId(ylShop.getId());
                //获取当前门店设备数量
                ylZeroShop.setShopQuantity(ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getDeviceType,1).eq(YlDevice::getBusinessId,ylShop.getId())));
                ylZeroShop.setYesterday(d);//因 统计是昨天的数据 所以创建 时间减一天 创建时间 要存昨天
                ylZeroShopService.save(ylZeroShop);
            }

        }


    }



    /** ylWeaningShopService  ylWeaningShopRecordService
     * APP运营端-门店-2级页面(撤机门店)(折线图用)
     * @param
     * @param
     */
    public void  addWeaningShop(){

       //先删除数据
        ylWeaningShopService.deleteWeaningShop();

        Date d= DateUtils.addDaysToDate(new Date(),-1);//取出 昨日 日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //查询 昨天 已解绑的设备店铺记录
        List<YlWeaningShopRecord> ylWeaningShopRecordList=    ylWeaningShopRecordService.list(new QueryWrapper<YlWeaningShopRecord>().eq("yesterday",dateFormat.format(d)));
        if (ylWeaningShopRecordList.size()>0){
            //重新 判断店铺有没有绑定设备
            for (YlWeaningShopRecord weaningShopRecord:ylWeaningShopRecordList) {
                Integer p=ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceType,1).eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getBusinessId,weaningShopRecord.getShopId()));
                if (p>0) {//说明此门店还有设备
                    ylWeaningShopRecordService.removeById(weaningShopRecord.getId());//删掉设备
                }
            }
        }
        //  获取   时间（2昨日、3本周、4本月） 撤机门店

        List<YlWeaningShopRecord> yesterdayShopList= ylWeaningShopRecordService.getUnbindShopList(2);//昨日
        if (yesterdayShopList.size()>0){
            //存储值
            for (YlWeaningShopRecord ylWeaningShopRecord:yesterdayShopList) {
                YlWeaningShop ylWeaningShop= new YlWeaningShop();
                ylWeaningShop.setBdId(ylWeaningShopRecord.getBdId());
                ylWeaningShop.setGroupId(ylWeaningShopRecord.getGroupId());
                ylWeaningShop.setShopId(ylWeaningShopRecord.getShopId());
                ylWeaningShop.setShopName(ylWeaningShopRecord.getShopName());
                ylWeaningShop.setUnbindTime(ylWeaningShopRecord.getUnbindTime());
                ylWeaningShop.setType(2);
                ylWeaningShopService.save(ylWeaningShop);
            }
        }
        List<YlWeaningShopRecord> weekShopList= ylWeaningShopRecordService.getUnbindShopList(3);//3本周
        if (weekShopList.size()>0){
            //存储值
            for (YlWeaningShopRecord ylWeaningShopRecord:weekShopList) {

                Integer p=ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceType,1).eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getBusinessId,ylWeaningShopRecord.getShopId()));
                if (p==0) {//说明此门店没有设备
                    YlWeaningShop ylWeaningShop= new YlWeaningShop();
                    ylWeaningShop.setBdId(ylWeaningShopRecord.getBdId());
                    ylWeaningShop.setGroupId(ylWeaningShopRecord.getGroupId());
                    ylWeaningShop.setShopId(ylWeaningShopRecord.getShopId());
                    ylWeaningShop.setShopName(ylWeaningShopRecord.getShopName());
                    ylWeaningShop.setUnbindTime(ylWeaningShopRecord.getUnbindTime());
                    ylWeaningShop.setType(3);
                    ylWeaningShopService.save(ylWeaningShop);
                }
            }
        }

        List<YlWeaningShopRecord> monthShopList= ylWeaningShopRecordService.getUnbindShopList(4);//4本月

        if (monthShopList.size()>0){
            //存储值
            for (YlWeaningShopRecord ylWeaningShopRecord:monthShopList) {
                Integer p=ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceType,1).eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getBusinessId,ylWeaningShopRecord.getShopId()));
                if (p==0) {//说明此门店没有设备
                    YlWeaningShop ylWeaningShop= new YlWeaningShop();
                    ylWeaningShop.setBdId(ylWeaningShopRecord.getBdId());
                    ylWeaningShop.setGroupId(ylWeaningShopRecord.getGroupId());
                    ylWeaningShop.setShopId(ylWeaningShopRecord.getShopId());
                    ylWeaningShop.setShopName(ylWeaningShopRecord.getShopName());
                    ylWeaningShop.setUnbindTime(ylWeaningShopRecord.getUnbindTime());
                    ylWeaningShop.setType(4);
                    ylWeaningShopService.save(ylWeaningShop);
                }
            }
        }

    }














    /**
     *
     * 门店数据
     * 1、 柜机门店数:
     * 指截止至昨目最后时刻绑定柜机设备的门店数
     * 2、0收益门店数:
     * 指截止至昨日最后时刻，连续铺设满7天，且连续7天
     * 的总收益小于等于0的总门店数
     *
     * 4、撤机门店数:
     * 指当月有过设备的门店，昨日设备数为0
     * 定时任务 生成门店统计数据
     * 5 净增门店数: 指(本月截止至昨日最后时刻的总门店数)-(上日最后时刻的总门店数)的差值
     *
     */
    @Override
    public void addHomeShopEnt() {

        //首先 删除旧数据
        baseMapper.deleteHomeShop();
        //1、柜机门店数
        //取出已绑定店铺的设备
        Date date= DateUtils.addDaysToDate(new Date(),-1);//取出昨天
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dateFormat.format(date));
        String jDate=dateFormat.format(date)+" 23:59:59";//昨天的最晚时间

        //YlDevice 查询 柜机已绑定设备的数据 device_status  和 时间最高值   List<YlDeviceRecords>  getMaxMac();
        List<String> stringList= ylDeviceMapper.getMaxMac(jDate);//获取正在绑定设备的最好数据
        if (stringList.size()>0){

            this.addHomeShopList(stringList,1);//执行新增
        }
        //求出 0收益门店数 指截止至昨日最后时刻，连续铺设满7天，且连续7天 的总收益小于等于0的总门店数
        //先求店铺 店铺必须帮设备 且大于7天
        List<String> stringShopList= ylDeviceMapper.getMaxDayShop();
        if (stringShopList.size()>0){
            List<String> stringShopIds=new LinkedList<>();//撤机门店id数组
            //说明有门店、 然后 查订单  查7天没有收益的店铺 坐统计
            for (String shopId: stringShopList) {
                //查店铺 7天没有订单数据的
                Integer quantity=  ylPowerBankOrderMapper.getShopQuantity(shopId);
                //当 数量为0的时候 为0收益门店
                if (quantity==0){//根据店铺id  获取店铺信息 business_id
                    stringShopIds.add(shopId);
                }
            }
            if (stringShopIds.size()>0){
                this.addHomeShopList(stringShopIds,2);//执行新增
            }
        }
        //新增门店 (按照新增门店通知 到时)

        //类型 1柜机门店数 2、0收益门店数、3 新铺门店数 4 撤机门店数 5净增门店数
        //4  撤机门店数:
        //指当月有过设备的门店，昨日设备数为0  bindingDeviceShop()
        List<YlDeviceRecords>deviceRecordsList= ylDeviceRecordsMapper.bindingDeviceShop();

        List<String> bindingDeviceShopIds=new LinkedList<>();//撤机门店id数组
        for (YlDeviceRecords deviceRecords:deviceRecordsList) {
            //说明当月有门店帮设备
            // 看看设备 有没有和店铺解绑
            String shopId=deviceRecords.getReceiveId();//店铺id
            String deviceId=deviceRecords.getDeviceId();//设备id
            //查询
            Integer p=ylDeviceMapper.selectCount(new LambdaQueryWrapper<YlDevice>().eq(YlDevice::getDeviceType,1).eq(YlDevice::getDeviceStatus,3).eq(YlDevice::getBusinessId,shopId));
            if (p==0){//说明此门店没有设备了
                bindingDeviceShopIds.add(shopId);
            }
        }
        if (bindingDeviceShopIds.size()>0){//说有有撤机门店
            //查 店铺数据
            this.addHomeShopList(bindingDeviceShopIds,4);//执行新增
        }
        //5 净增门店数:
        //指(本月截止至昨日最后时刻的总门店数)-(上日最后时刻的总门店数)的差值
        //
        List<YlShop>  shopList=   ylShopService.getLastMonthShop();//上月数据
        for (YlShop ylShop:shopList) {
            //生成数据  柜机门店数
            YlHomeShop ylHomeShop=new YlHomeShop();
            if (oConvertUtils.isNotEmpty(ylShop.getBdId())){
                ylHomeShop.setBdId(ylShop.getBdId());
                ylHomeShop.setGroupId(ylShop.getOrgId());
                ylHomeShop.setType(5);
                ylHomeShop.setShopQuantity(1);
                ylHomeShop.setMark(2);// 1 本月店铺数据  2、上个月店铺数据
                baseMapper.insert(ylHomeShop);
            }
        }
        List<YlShop>  thisMonthShop=  ylShopService.getThisMonthShop(jDate);
        for (YlShop ylShop:thisMonthShop) {
            //生成数据  柜机门店数
            YlHomeShop ylHomeShop=new YlHomeShop();
            ylHomeShop.setBdId(ylShop.getBdId());
            ylHomeShop.setGroupId(ylShop.getOrgId());
            ylHomeShop.setType(5);
            ylHomeShop.setShopQuantity(1);
            ylHomeShop.setMark(1);// 1 本月店铺数据  2、上个月店铺数据
            baseMapper.insert(ylHomeShop);
        }



        //APP运营端-门店-2级页面(撤机门店)(折线图用)
        //查询 2昨日、3本周、4本月  所有 撤机的本地  ylDeviceRecordsMapper.bindingDeviceShop();
        this.addWeaningShop();










    }
}
